package cn.schoolwow.util.domain.query.instanceList;

import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.module.query.instanceList.domain.InstanceListQueryOption;
import cn.schoolwow.util.module.query.instanceList.flow.get.GetInstanceMultipleFieldValueListBusiness;
import cn.schoolwow.util.module.query.instanceList.flow.get.GetInstanceSingleFieldValueListBusiness;
import cn.schoolwow.util.module.query.instanceList.flow.group.GroupInstanceByFieldBusiness;
import cn.schoolwow.util.module.query.instanceList.service.FilterInstanceListCompositeBusiness;

import java.util.*;

public class QueryInstanceListResponse<T> {
    private InstanceListQueryOption<T> instanceQueryOption;

    public QueryInstanceListResponse(InstanceListQueryOption<T> instanceQueryOption) {
        this.instanceQueryOption = instanceQueryOption;
    }

    /**获取个数*/
    public long count(){
        List<T> filterInstanceList = getList();
        return null==filterInstanceList?0:filterInstanceList.size();
    }

    /**获取第一个值*/
    public <T> T getOne(){
        List<T> filterInstanceList = getList();
        if(null==filterInstanceList||filterInstanceList.isEmpty()){
            return null;
        }
        return filterInstanceList.get(0);
    }

    /**获取有且仅有一个过滤值,否则抛出异常*/
    public <T> T checkOne(){
        List<T> filterInstanceList = getList();

        String condition = null;
        if(null!=instanceQueryOption.singleValue||null!=instanceQueryOption.singleValueList){
            List list = new ArrayList();
            if(null!=instanceQueryOption.singleValue){
                list.add(instanceQueryOption.singleValue);
            }
            if(null!=instanceQueryOption.singleValueList){
                list.addAll(instanceQueryOption.singleValueList);
            }
            condition = list.toString();
        }else{
            Map<Collection<String>,Object> conditionMap = new HashMap<>();
            conditionMap.putAll(instanceQueryOption.fieldValueMap);
            conditionMap.putAll(instanceQueryOption.fieldValueCollectionMap);
            condition = conditionMap.toString();
        }

        if(null==filterInstanceList||filterInstanceList.size()==0){
            throw new IllegalArgumentException("符合条件的实例为空!"+"查询条件:"+condition);
        }
        if(filterInstanceList.size()>1){
            throw new IllegalArgumentException("期望获取1个实例，实际个数:"+filterInstanceList.size()+"!查询条件:"+condition);
        }
        return filterInstanceList.get(0);
    }

    /**获取过滤后的列表*/
    public <T> List<T> getList(){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterInstanceListCompositeBusiness())
                .putInstanceData(instanceQueryOption)
                .execute();
        List<T> filterInstanceList = (List<T>) flowContext.getData("filterInstanceList");
        return filterInstanceList;
    }

    /**获取指定字段的值*/
    public List getSingleFieldValueList(String fieldName){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterInstanceListCompositeBusiness())
                .next(new GetInstanceSingleFieldValueListBusiness())
                .putInstanceData(instanceQueryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .execute();
        List fieldValueList = (List) flowContext.getData("fieldValueList");
        return fieldValueList;
    }

    /**获取指定多个字段的值*/
    public List<String> getMultipleFieldValueList(String... fieldNames){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterInstanceListCompositeBusiness())
                .next(new GetInstanceMultipleFieldValueListBusiness())
                .putInstanceData(instanceQueryOption)
                .putCurrentCompositeFlowData("fieldNames", fieldNames)
                .execute();
        List fieldValueList = (List) flowContext.getData("fieldValueList");
        return fieldValueList;
    }

    /**获取指定多个字段的值*/
    public List<String> getMultipleFieldValueList(Collection<String> fieldNames){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterInstanceListCompositeBusiness())
                .next(new GetInstanceMultipleFieldValueListBusiness())
                .putInstanceData(instanceQueryOption)
                .putCurrentCompositeFlowData("fieldNames", fieldNames.toArray(new String[0]))
                .execute();
        List fieldValueList = (List) flowContext.getData("fieldValueList");
        return fieldValueList;
    }

    /**根据字段字段分组*/
    public <P,T> Map<P,List<T>> groupByFieldName(String fieldName, Class<P> fieldClass){
        FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterInstanceListCompositeBusiness())
                .next(new GroupInstanceByFieldBusiness<P,T>())
                .putInstanceData(instanceQueryOption)
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .execute();
        Map<P,List<T>> fieldInstanceListMap = (Map<P, List<T>>) flowContext.getData("fieldInstanceListMap");
        return fieldInstanceListMap;
    }
}
